package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

//@RestController
@Controller
@RequestMapping("/json")
public class JosnController {

    // http://localhost:8080/json/test1?id=1&name=zzz&gender=false&date=2018-12-12 12:20:30
    @RequestMapping("/test1")
    @ResponseBody// 添加该注解，代表返回的内容不是页面了，而是方法返回值类型对象对应的josn字符串
    public User test1(User user){
        return user;
    }

    @RequestMapping("/test2")
    @ResponseBody
    public List<User> test2(User user){
        List<User> list = Arrays.asList(user,user);
        return list;
    }

    @RequestMapping(value = "/test3",produces = "text/html;charset=utf-8")// produces属性用于防止返回的字符串中文乱码
    @ResponseBody
    public String test3(){
        System.out.println("dddddddddd");
        return "你好 json";
    }

    @RequestMapping("/test4")
    @ResponseBody
    public User test4(@RequestBody User user){//接收请求体中的Json参数,并且转换成指定的对象
        System.out.println(user);
        return user;
    }
}
